package com.yan.studentsystem;

import java.util.ArrayList;
import java.util.Scanner;

public class Manage {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final ArrayList<Student> studentsList = new ArrayList<>();

    public void addStudent() {
        System.out.println("请输入学生学号");
        String sid = INPUT.next();
        int index = getIndex(sid);
        if(index != -1 & studentsList.size() != 0){
            System.out.println("该学号已存在,请重新输入");
            return;
        }
        System.out.println("请输入学生姓名");
        String name = INPUT.next();
        System.out.println("请输入学生年龄");
        int age = INPUT.nextInt();
        System.out.println("请输入学生居住地");
        String address = INPUT.next();
        Student stu = new Student(sid , name , age , address);
        studentsList.add(stu);
        System.out.println("添加成功");
    }

    public void findStudent(){
        if(studentsList.size() == 0){
            System.out.println("无信息，请先添加信息在查询");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t居住地址\t");
        for (Student student : studentsList) {
            System.out.println(student.getSid() + "\t\t" + student.getName() + "\t\t"+ student.getAge() + "岁\t\t"+ student.getaddress());
        }
    }

    public void upDate(){
        System.out.println("请输入要修改的学号");
        String id = INPUT.next();
        int index = getIndex(id);
        if(index != - 1){
            System.out.println("请输入学生新姓名");
            String newName = INPUT.next();
            studentsList.get(index).setName(newName);
            System.out.println("请输入学生新年龄");
            int newAge = INPUT.nextInt();
            studentsList.get(index).setAge(newAge);
            System.out.println("请输入学生新居住地址");
            String newAddress = INPUT.next();
            studentsList.get(index).setaddress(newAddress);
            System.out.println("修改学生成功");
        }else {
            System.out.println("信息不存在");
        }
    }

    public int getIndex( String target) {
        for (int i = 0; i < studentsList.size(); i++) {
            if(studentsList.get(i).getSid().equals(target)){
                return i;
            }
        }
        return -1;
    }

    public void deletDate(){
        System.out.println("请输入要删除的学生学号");
        String id = INPUT.next(); //控制层输入
        int index = getIndex(id);
        if(index != -1){
            studentsList.remove(studentsList.get(index));
            System.out.println("删除成功");
        } else {
            System.out.println("该信息不存在,请重新输入");
        }
    }
}
